'''
Created on May 25, 2009

@author: user
'''

from cliques.potentialBasedSearch import potentialBasedSearch
import random
import math

class totalMembershipSearch(potentialBasedSearch):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        potentialBasedSearch.__init__(self)
        self.membership = dict()

    def choose_node(self):
        max_value = -1
        max_node = None
        for node in self.generated:
            value = self.membership_value(node)
            if max_value<value:
                max_value=value
                max_node=node
        return max_node
            
    def membership_value(self,node):
        self.membership.clear()
                
        for kPc in self.node_to_kPc[node]:
            core_size = len(kPc.core)
            if core_size not in self.membership:
                self.membership[core_size]=1
            else:
                self.membership[core_size]=self.membership[core_size]+1
        
        sum=0
        for core_size,count in self.membership:
            sum=sum+math.pow(core_size, count)
        return sum
         
    def clique_found(self):
        for kPc in self.pcdb:
            if self.current_node in kPc.core and len(kPc.core)==self.k-1:
                self.done = True
                return True
        return False

    def __str__(self):
        return "totalMembershipSearch"